#include "scence.hpp"

#include "g_comm.hpp"

#include "player.hpp"

#define CHECK_GAME_STATUS \
	if (gateway::G_PLAYING != _game_status) \
	{ \
		return 0; \
	}

IMPL_LOGGER(Player, logger);

Player::Player(Connection & conn, const RoleData & role_data)
	: _conn(&conn),
	  _role_data(role_data),
	  _game_status(gateway::G_STANDUP),
	  _position()
{
}

Player::~Player()
{
}

void Player::sitdown(const Position & position)
{
	this->position(position);
}

void Player::standup()
{
	if (gateway::G_SITDOWN == _game_status)
	{
		_scence->cancel_seat(_position._room_id);
	}
	else
	{
		LOG4CPLUS_ERROR(logger, "player isn't at sitdown status. id: " << _role_data._id << ", name: " << _role_data._name);
	}
}

template <>
int Player::handle(Connection & conn, SCMJ2EnterRoomRes & res)
{
	if (0 != res._err_code)
	{
		// @TODO enter room failed.
		return 0;
	}
	// @TODO

#if 0
	SCMJ2EnterRoomRes room_res;

	RoleB role_b;
	fill_role_b(role_b);
	room_res._body._role_list.push_back(role_b);

	PlayerMgr::instance().fill_table(room_res._body._role_list);

	Gateway::instance().send_to_player(player, room_res);
	
	PlayerMgr::instance().send_player_to_table(player, body._role_list);
#endif
	return 0;
}

template <>
int Player::handle(Connection & conn, CSMJ2PutCardReq & req)
{
#if 0
	CHECK_GAME_STATUS;

	const CSPutCardReqBody & body = req._body;

	Gateway::instance().send_to_scence(player->_scence, (void*)&req);
#endif
	return 0;
}

template <>
int Player::handle(Connection & conn, SCMJ2PutCardRes & res)
{
#if 0
	CHECK_GAME_STATUS;

	const SSPutCardResBody & body = res._body;

	SCPutCardRes card_res;
	card_res._err_code = body._err_code;

	Gateway::instance().send_to_player(player, (void*)&card_res);

	if (0 != body._err_code)
	{
		return 0;
	}

	SCNotifyPutCard notify;
	notify._rid   = body._rid;
	nofity._card  = body._card;

	std::vector<RID>::iterator iter = body._notify_list.begin();
	for (; iter != body._notify_list.end(); ++ iter)
	{
		Player * player = PlayerMgr::instance().find_player(*iter);
		if (NULL == player)
		{
			// @TODO log error
			continue;
		}

		Gateway::instance().send_to_player(player, (void*)&notify);
	}
#endif
	return 0;
}

template <>
int Player::handle(Connection & conn, CSMJ2GetCardReq & req)
{
	return 0;
}

template <>
int Player::handle(Connection & conn, SCMJ2GetCardRes & res)
{
	return 0;
}

template <>
int Player::handle(Connection & conn, CSMJ2ChiReq & req)
{
	return 0;
}

template <>
int Player::handle(Connection & conn, SCMJ2ChiRes & res)
{
	return 0;
}

template <>
int Player::handle(Connection & conn, CSMJ2PengReq & req)
{
	return 0;
}

template <>
int Player::handle(Connection & conn, SCMJ2PengRes & res)
{
	return 0;
}

template <>
int Player::handle(Connection & conn, CSMJ2MingGangReq & req)
{
	return 0;
}

template <>
int Player::handle(Connection & conn, SCMJ2MingGangRes & res)
{
	return 0;
}

template <>
int Player::handle(Connection & conn, CSMJ2AnGangReq & req)
{
	return 0;
}

template <>
int Player::handle(Connection & conn, SCMJ2AnGangRes & res)
{
	return 0;
}


